package com.extweb4j.core.model;

import com.extweb4j.core.kit.ExtKit;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhaoWeinan
 * @date 2019/1/6
 * @description
 */
public class ExtDepartment extends CoreModel<ExtDepartment> {

    private static final long serialVersionUID = 1L;

    public static ExtDepartment dao = new ExtDepartment();

    private static final String SELECT_SQL = "SELECT DeptId, DeptNo, DeptName, ParentDeptId, FlowRunId, OrderIndex, OrganizeCode, RegisterCode, " +
            "OrganizeType, OrganizeAddress, ScopeOfBusiness, Industry, BankNo, Bank, BankArea, Area, Person, " +
            "RegisterPrincipal, ExistenceTime, BusinessTimelimitStart, BusinessIssueOrganize, PresideName, " +
            "PresideMobile, PresideLinkTel, PresideEmail, PresideTitle, FaxNo, PostalAddress, PostalNo, " +
            "State, IsPassed, IsDeleted, DeptType, Guid, CheckUser, CheckTime, RegisterTime, IsForbid, ForbidType, " +
            "ForbidSTime, ForbidETime, Reason, Remark ";

    private static final String FROM_SQL = " FROM Department ";

    private static final String WHERE_SQL = " WHERE 1=1 ";

    public boolean isExist(String str) {
        // TODO Auto-generated method stub
        return super.findBy("Id = ?", str) == null;
    }

    public Page<ExtDepartment> pageDepartmentBy(int page, int limit, String keywords) {
        // TODO Auto-generated method stub


        StringBuffer form = new StringBuffer();

        form.append(FROM_SQL);
        form.append(WHERE_SQL);
        form.append(" AND DeptType = 0 ");

        List<Object> paras = new ArrayList<Object>();
        if (StringUtils.isNotBlank(keywords)) {
            keywords = ExtKit.sqlFilterValidate(keywords);
            form.append(" AND (DeptName LIKE '%" + keywords + "%' OR ScopeOfBusiness LIKE '%" + keywords + "%' ");
            form.append(" OR OrganizeCode LIKE '%" + keywords + "%' )");
        }
        form.append(" ORDER BY RegisterTime DESC");
        return super.paginate(page, limit, SELECT_SQL, form.toString(), paras.toArray());
    }

    /**
     * 根据id 获取新闻实体类
     *
     * @return
     * @Param id
     */
    public ExtDepartment findDepartmentById(String id) {
        // TODO Auto-generated method stub
        return ExtDepartment.dao.findFirst("SELECT * FROM  Department where DeptId=?", id);
    }

    public ExtUser getUserNameByUserId(String id) {
        return ExtUser.dao.findFirst("SELECT * FROM  Department where id=?", id);
    }

    public String getId() {
        return this.getStr("id");
    }
}
